package cn.oms.manage.dao.persist.repository.impl;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.util.PageInfoToPageDataConverter;
import cn.oms.manage.dao.persist.mapper.CustomerMapper;
import cn.oms.manage.dao.persist.repository.ICustomerRepository;
import cn.oms.manage.pojo.entity.Customer;
import cn.oms.manage.pojo.vo.CustomerListItemVO;
import cn.oms.manage.pojo.vo.CustomerStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class CustomerRepositoryImpl implements ICustomerRepository {
    
    @Autowired
    private CustomerMapper customerMapper;

    public CustomerRepositoryImpl() {
        log.debug("创建数据访问实现类对象：CustomerRepositoryImpl");
    }

    @Override
    public int insert(Customer customer) {
        log.debug("开始执行【插入订单】的数据访问，参数：{}", customer);
        return customerMapper.insert(customer);
    }

    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除用户】的数据访问，参数：{}", id);
        return customerMapper.deleteById(id);
    }

    @Override
    public void updateDepartmentNameByDepartmentId(Long id, String name) {
        log.debug("开始执行【根据科室id修改所有科室名称】的数据访问，参数：{}，{}", id,name);
        customerMapper.updateDepartmentNameByDepartmentId(id,name);
    }

    @Override
    public int update(Customer customer) {
        log.debug("开始执行【更新用户】的数据访问，参数：{}", customer);
        return customerMapper.updateById(customer);
    }

    @Override
    public int countByDepartmentId(Long departmentId) {
        log.debug("开始执行【统计匹配相册的图片的数量】的数据访问，相册：{}", departmentId);
        QueryWrapper<Customer> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("department_id", departmentId);
        return customerMapper.selectCount(queryWrapper);
    }

    @Override
    public CustomerStandardVO getStandardById(Long id) {
        log.debug("开始执行【根据ID查询用户信息】的数据访问，参数：{}", id);
        return customerMapper.getStandardById(id);
    }

    @Override
    public PageData<CustomerListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询用户列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<CustomerListItemVO> list = customerMapper.list();
        PageInfo<CustomerListItemVO> pageInfo = new PageInfo<>(list);
        PageData<CustomerListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
